package com.example08;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;

public class IOTest {
    public static void main(String[] args) {
        String fileName = "C:/example/from.txt";
        // \r\n在txt文本中换行
        String str = "白日依山尽\r\n" +
                        "黄河入海流\r\n" +
                        "欲穷千里目\r\n" +
                        "更上一层楼\r\n";
        String toFile = "C:/example/to.txt";
        String dir = "C:/example";
        String toFile2 = "C:/example/nio/to.txt";
         try {
             System.out.println("----- 创建文件 ------");
             //createFile(fileName);
             System.out.println("-----  将字符串写入文件 -------");
             //writeToFile(fileName, str);
             //writeToFile2(fileName, str);
             System.out.println("--------- 基于基本IO流实现文件的复制 ----------");
             //copyByIO(fileName, toFile);
             System.out.println("--------- 基于NIO实现文件的复制 ----------");
             //copyByNIO(fileName, toFile2);
             System.out.println("---------- 删除指定文件 -------------");
             //deleteFile(toFile);
             System.out.println("---------- 遍历指定目录文件 -------------");
             walkDirectories(dir);
         }catch (IOException e) {
             System.out.println(e.getMessage());
         }
    }
    private static void createFile(String fileName) throws IOException {
        Path path = Path.of(fileName);
        Files.createDirectories(path.getParent());
        Files.createFile(path);
    }
    private static void writeToFile(String fileName, String content) throws IOException {
        try(FileOutputStream file = new FileOutputStream("C:/example/from.txt")) {
            byte[] bytes = content.getBytes();
            file.write(bytes);
        }
    }
    private static void writeToFile2(String fileName, String content) throws IOException{
        Path path = Path.of(fileName);
        Files.createDirectories(path.getParent());
        Files.createFile(path);
        Files.write(path, content.getBytes());
    }
    private static void copyByIO(String sourceFile, String targetFile) {
        try(InputStream input = new FileInputStream(sourceFile);
            OutputStream output = new FileOutputStream(targetFile)) {
            byte[] bytes = new byte[4];
            int i = 0, len;
            while((len = input.read(bytes)) != -1) {
                output.write(bytes, 0, len);
                i++;
            }
            System.out.println("基于IO，复制成功，读写循环次数为" + i + "次!");
        }catch (IOException e) {
            System.out.println(e.getMessage());
        }


    }
    private static void copyByNIO(String sourceFile, String targetFile) throws IOException {
        Path source = Paths.get(sourceFile);
        Path target = Paths.get(targetFile);
        Files.createDirectories(target.getParent());
        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
        System.out.println("基于NIO，复制成功");
    }
    private static void deleteFile(String fileName) throws IOException {
        Path path = Path.of(fileName);
        Files.delete(path);
        System.out.println("删除" + fileName + "成功!");
    }
    private static void walkDirectories(String dir) throws IOException{
        Path path = Paths.get(dir);
        Files.walk(path)
                .forEach(System.out::println);
    }
}

